// pages/my/my.js
var httpTool = require('../..//utils/network.js');
var util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    vacationInfo: '',
    phone: '',
    code: '',
    time: '',
    currentTime: 60,
    disabled: false
  },
  onLoad: function(options) {
    // 生命周期函数--监听页面加载
    if (app.globalData.vacationInfo) {
      this.setData({
        vacationInfo: app.globalData.vacationInfo,
      })
    }
    var vacationId = options.vacationId ? options.vacationId : app.globalData.vacationId;
    var taskId = options.taskId;
    // data["taskId"] = taskId;
    // data["memberId"] = app.userData.userId;
    // data["memberPhone"] = app.globalData.userInfo.userName;
    // data["vacationId"] = vacationId;
    this.setData({
      taskId: taskId,
      memberId: app.userData.userId,
      memberPhone: app.globalData.userInfo.userName,
      vacationId: vacationId,
    })
  },
  fieldListen: function(event) {
    let field = event.currentTarget.dataset['field'];
    let value = event.detail.value;
    console.log("field:" + field + ",value:" + value);
    let data = this.data;
    data[field] = value;
    this.setData(data)
  },
  
  //发送验证码
  sendSms: function () {
    console.log('sendSms')
    var phone = this.data.phone;
    if (!phone) {
      util.fxAlert('请输入手机号')
      return;
    }
    var regNum = new RegExp('^1[3-9][0-9]{9}$');
    if (!regNum.test(phone)) {
      util.fxAlert('请输入正确的手机号')
      return;
    }
    if (this.data.disabled) {
      return;
    }
    var that = this;
    // 0注册  1忘记密码 2 签收验证
    // var type = this.data.type == 0 ? 0 : 1
    var param = {
      phone: phone,
      type: 3,
      //saasId: app.globalData.saasId,
      //shopId: app.userData.shopId,
    }
    var url = app.data.httpUrl + "v1/sms/sendSmsCode";
    httpTool.sendRequest(
      url, param, '发送中...',
      function (res) {
        console.log(res)
        if (res.code == 200) {
          util.fxAlert('发送成功')
          that.setData({
            disabled: true,
          })
          that.getCode();
        }
      },
      function (e) {
        util.fxAlert(e.msg)
      }
    )
  },
  //验证码倒计时函数
  getCode: function () {
    var that = this;
    var currentTime = that.data.currentTime;
    that.setData({
      time: currentTime
    })
    var interval = setInterval(function () {
      that.setData({
        time: (currentTime - 1)
      })
      currentTime--;
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '',
          currentTime: 60,
          disabled: false
        })
      }
    }, 1000)
  },

  /**
   * 确认签收断言验证
   */
  assertConfirmSign: function() {
    var data = this.data;
    if (!data.childrenName) {
      util.fxAlert("请输入孩子姓名");
      return false;
    }
    if (!data.age) {
      util.fxAlert("请输入年龄");
      return false;
    }
    if (!data.sex) {
      util.fxAlert("请选择性别");
      return false;
    }
    if (!data.grade) {
      util.fxAlert("请输入年级");
      return false;
    }
    if (!data.age) {
      util.fxAlert("请选择通讯地址");
      return false;
    }
    if (!data.address) {
      util.fxAlert("请输入年龄");
      return false;
    }
    if (!data.guardianName) {
      util.fxAlert("请输入监护人姓名");
      return false;
    }
    if (!data.guardianRelation) {
      util.fxAlert("请输入与孩子关系");
      return false;
    }
    if (!data.phone) {
      util.fxAlert("请输入手机号");
      return false;
    }
    // if (!data.age) {
    //   util.fxAlert("请输入验证码");
    //   return false;
    // }
    return true;
  },
  //确认签收
  confirmSign: function() {
    var that = this
    var url = app.data.httpUrl + 'v1/signature/saveOrUpdate'
    var param = that.data;
    console.log(param);
    if (!this.assertConfirmSign()) {
      return;
    }
    httpTool.sendRequest(url, param, '提交中...', function(res) {
      if (res.code == 200) {
        util.fxAlert('签收成功！')
        wx.navigateBack()
      } else {
        util.fxAlert(res.msg)
      }
    }, function(error) {
      util.fxAlert(error.msg);
    });
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function() {
    // 生命周期函数--监听页面显示

  },
  onHide: function() {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function() {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数

  },
  onShareAppMessage: function() {
    // 用户点击右上角分享
    return {
      title: 'title', // 分享标题
      desc: 'desc', // 分享描述
      path: 'path' // 分享路径
    }
  }
})